home *** CD-ROM | disk | FTP | other *** search
/ Apple Developer Connection Student Program / ADC Tools Sampler CD Disk 3 1999.iso / Metrowerks CodeWarrior / Java Support / Java_Source / Java2 / src / java / security / MessageDigestSpi.java < prev    next >
Encoding:
Java Source  |  1999-05-28  |  5.2 KB  |  162 lines  |  [TEXT/CWIE]

  1. /*
  2.  * @(#)MessageDigestSpi.java    1.5 98/07/24
  3.  *
  4.  * Copyright 1997, 1998 by Sun Microsystems, Inc.,
  5.  * 901 San Antonio Road, Palo Alto, California, 94303, U.S.A.
  6.  * All rights reserved.
  7.  *
  8.  * This software is the confidential and proprietary information
  9.  * of Sun Microsystems, Inc. ("Confidential Information").  You
  10.  * shall not disclose such Confidential Information and shall use
  11.  * it only in accordance with the terms of the license agreement
  12.  * you entered into with Sun.
  13.  */
  14.  
  15. package java.security;
  16.  
  17. import java.util.*;
  18. import java.lang.*;
  19. import java.io.IOException;
  20. import java.io.ByteArrayOutputStream;
  21. import java.io.PrintStream;
  22. import java.io.InputStream;
  23. import java.io.ByteArrayInputStream;
  24.  
  25. /**
  26.  * This class defines the <i>Service Provider Interface</i> (<b>SPI</b>)
  27.  * for the <code>MessageDigest</code> class, which provides the functionality
  28.  * of a message digest algorithm, such as MD5 or SHA. Message digests are
  29.  * secure one-way hash functions that take arbitrary-sized data and output a
  30.  * fixed-length hash value.
  31.  *
  32.  * <p> All the abstract methods in this class must be implemented by a
  33.  * cryptographic service provider who wishes to supply the implementation
  34.  * of a particular message digest algorithm.
  35.  *
  36.  * <p> Implementations are free to implement the Cloneable interface.
  37.  *
  38.  * @author Benjamin Renaud 
  39.  *
  40.  * @version 1.5 99/03/26
  41.  *
  42.  * @see MessageDigest
  43.  */
  44.  
  45. public abstract class MessageDigestSpi {
  46.  
  47.     /**
  48.      * Returns the digest length in bytes.
  49.      *
  50.      * <p>This concrete method has been added to this previously-defined
  51.      * abstract class. (For backwards compatibility, it cannot be abstract.)
  52.      * 
  53.      * <p>The default behavior is to return 0.
  54.      * 
  55.      * <p>This method may be overridden by a provider to return the digest
  56.      * length.
  57.      *
  58.      * @return the digest length in bytes.
  59.      *
  60.      * @since JDK1.2
  61.      */
  62.     protected int engineGetDigestLength() {
  63.     return 0;
  64.     }
  65.  
  66.     /**
  67.      * Updates the digest using the specified byte.
  68.      *
  69.      * @param input the byte to use for the update.
  70.      */
  71.     protected abstract void engineUpdate(byte input);
  72.  
  73.     /**
  74.      * Updates the digest using the specified array of bytes,    
  75.      * starting at the specified offset.
  76.      *
  77.      * @param input the array of bytes to use for the update.
  78.      *
  79.      * @param offset the offset to start from in the array of bytes.
  80.      *
  81.      * @param len the number of bytes to use, starting at 
  82.      * <code>offset</code>.
  83.      */
  84.     protected abstract void engineUpdate(byte[] input, int offset, int len);
  85.  
  86.     /**
  87.      * Completes the hash computation by performing final
  88.      * operations such as padding. Once <code>engineDigest</code> has 
  89.      * been called, the engine should be reset (see 
  90.      * {@link engineReset() engineReset}).  
  91.      * Resetting is the responsibility of the
  92.      * engine implementor.
  93.      *
  94.      * @return the array of bytes for the resulting hash value.  
  95.      */
  96.     protected abstract byte[] engineDigest();
  97.  
  98.     /**
  99.      * Completes the hash computation by performing final
  100.      * operations such as padding. Once <code>engineDigest</code> has
  101.      * been called, the engine should be reset (see 
  102.      * {@link engineReset() engineReset}).  
  103.      * Resetting is the responsibility of the
  104.      * engine implementor.
  105.      *
  106.      * This method should be abstract, but we leave it concrete for
  107.      * binary compatibility.  Knowledgeable providers should override this
  108.      * method.
  109.      *
  110.      * @param buf the output buffer in which to store the digest
  111.      *
  112.      * @param offset offset to start from in the output buffer
  113.      *
  114.      * @param len number of bytes within buf allotted for the digest.
  115.      * Both this default implementation and the SUN provider do not
  116.      * return partial digests.  The presence of this parameter is solely
  117.      * for consistency in our API's.  If the value of this parameter is less
  118.      * than the actual digest length, the method will throw a DigestException.
  119.      * This parameter is ignored if its value is greater than or equal to
  120.      * the actual digest length.
  121.      *
  122.      * @return the length of the digest stored in the output buffer.
  123.      * 
  124.      * @exception DigestException if an error occurs.
  125.      *
  126.      * @since JDK1.2
  127.      */
  128.     protected int engineDigest(byte[] buf, int offset, int len)
  129.                         throws DigestException {
  130.  
  131.     byte[] digest = engineDigest();
  132.     if (len < digest.length)
  133.         throw new DigestException("partial digests not returned");
  134.     if (buf.length - offset < digest.length)
  135.         throw new DigestException("insufficient space in the output "
  136.                       + "buffer to store the digest");
  137.     System.arraycopy(digest, 0, buf, offset, digest.length);
  138.     return digest.length;
  139.     }
  140.  
  141.     /**
  142.      * Resets the digest for further use.
  143.      */
  144.     protected abstract void engineReset();    
  145.  
  146.     /**    
  147.      * Returns a clone if the implementation is cloneable.    
  148.      * 
  149.      * @return a clone if the implementation is cloneable.
  150.      *
  151.      * @exception CloneNotSupportedException if this is called on an
  152.      * implementation that does not support <code>Cloneable</code>.
  153.      */
  154.     public Object clone() throws CloneNotSupportedException {
  155.     if (this instanceof Cloneable) {
  156.         return super.clone();
  157.     } else {
  158.         throw new CloneNotSupportedException();
  159.     }
  160.     }
  161. }
  162.